const mongoose = require('mongoose');

const foodSchema = mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      trim: true,
    },
    categoryId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'FoodCategory',
      required: true,
      validate: {
        validator: function(v) {
          return v != null;
        },
        message: '食物分类ID不能为空'
      }
    },
    image: {
      type: String,
      trim: true,
    },
    unit: {
      type: String,
      default: '克',
      trim: true,
    },
    calories: {
      type: Number,
      required: true,
      min: 0,
    },
    nutrition: {
      protein: {
        type: Number,
        default: 0,
        min: 0,
      },
      fat: {
        type: Number,
        default: 0,
        min: 0,
      },
      carbohydrate: {
        type: Number,
        default: 0,
        min: 0,
      },
      fiber: {
        type: Number,
        default: 0,
        min: 0,
      },
      sugar: {
        type: Number,
        default: 0,
        min: 0,
      },
      sodium: {
        type: Number,
        default: 0,
        min: 0,
      },
      cholesterol: {
        type: Number,
        default: 0,
        min: 0,
      },
      vitamins: {
        A: { type: Number, default: 0, min: 0 },
        C: { type: Number, default: 0, min: 0 },
        D: { type: Number, default: 0, min: 0 },
        E: { type: Number, default: 0, min: 0 },
        K: { type: Number, default: 0, min: 0 },
        B1: { type: Number, default: 0, min: 0 },
        B2: { type: Number, default: 0, min: 0 },
        B6: { type: Number, default: 0, min: 0 },
        B12: { type: Number, default: 0, min: 0 },
      },
      minerals: {
        calcium: { type: Number, default: 0, min: 0 },
        iron: { type: Number, default: 0, min: 0 },
        magnesium: { type: Number, default: 0, min: 0 },
        potassium: { type: Number, default: 0, min: 0 },
        zinc: { type: Number, default: 0, min: 0 },
      }
    },
    isVerified: {
      type: Boolean,
      default: false,
    },
    source: {
      type: String,
      trim: true,
    },
    isDeleted: {
      type: Boolean,
      default: false,
    }
  },
  {
    timestamps: true,
  }
);

/**
 * @typedef Food
 */
const Food = mongoose.model('Food', foodSchema);

module.exports = Food; 